package com.xtravel.common.adapter;

import android.os.Handler;
import android.os.Message;
import android.widget.BaseAdapter;
import com.xtravel.common.database.dto.DtoBase;

import java.util.ArrayList;
import java.util.List;

public abstract class BaseDataAdapter<T extends DtoBase> extends BaseAdapter {
    protected List<T> dataList;
    private Handler dataCallBackHanlder;
    protected Handler apiCallBackHanlder = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            List<T> datas = (List<T>) msg.obj;
            updateData(datas);
            if (dataCallBackHanlder != null) {
                Message newMsg = new Message();
                newMsg.what = 2;
                newMsg.obj = datas;
                dataCallBackHanlder.sendMessage(newMsg);
            }
        }
    };

    public BaseDataAdapter() {
    }

    public BaseDataAdapter(List<T> data) {
        this.dataList = data;
    }

    public BaseDataAdapter(List<T> data, Handler dataCallBackHanlder) {
        this.dataList = data;
        this.dataCallBackHanlder = dataCallBackHanlder;
    }

    @Override
    public int getCount() {
        if (dataList == null) {
            return 0;
        }
        return dataList.size();
    }

    @Override
    public T getItem(int position) {
        if (dataList == null) {
            return null;
        }
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    protected void clearData() {
        if (dataList != null)
            dataList.clear();
    }

    public void addAtBegin(T item) {
        if (dataList == null) {
            dataList = new ArrayList<T>();
        }

        if (item == null)
            return;
        dataList.add(0, item);
    }

    public void addRangeAtBegin(List<T> items) {
        if (dataList == null) {
            dataList = new ArrayList<T>();
        }

        if (items == null || items.size() == 0)
            return;
        dataList.addAll(0, items);
    }

    public void addAtEnd(T item) {
        if (dataList == null) {
            dataList = new ArrayList<T>();
        }

        if (item == null)
            return;
        dataList.add(item);
    }

    public void addRangeAtEnd(List<T> items) {
        if (dataList == null) {
            dataList = new ArrayList<T>();
        }
        dataList.addAll(items);
    }

    public boolean isExist(T item) {
        if (dataList == null) {
            return false;
        }
        for (T it : dataList) {
            if (it.id == item.id) {
                return true;
            }
        }

        return false;
    }

    public Long getMaxId() {
        Long maxId = -1L;
        if (dataList == null) {
            return maxId;
        }

        for (T item : dataList) {
            if (item.objId != null && item.objId > maxId) {
                maxId = item.objId;
            }
        }

        return maxId;
    }

    public Long getMinId() {
        Long minId = Long.MAX_VALUE;
        if (dataList == null) {
            return minId;
        }

        for (T item : dataList) {
            if (item.objId != null && item.objId < minId)
                minId = item.objId;
        }

        return minId;
    }

    public abstract void getData(Object args);

    protected abstract void updateData(List<T> data);

    @Override
    public boolean isEmpty() {
        if (dataList == null) {
            return false;
        }
        return dataList.size() == 0;
    }
}
